import SwiftUI

/// 按钮型号：大中小
enum ButtonSizeEnum {
    case large
    case medium
    case small
}

/// 按钮类型：主要、次要、警示、文字
enum ButtonTypeEnum   {
    case primary
    case secondary
    case danger
    case character
}

/// 按钮宽度、高度、内边距
struct ButtonSize {
    var width:CGFloat
    var height:CGFloat
    var edgeInsets:EdgeInsets
    
    init(width: CGFloat, height: CGFloat, edgeInsets: EdgeInsets) {
        self.width = width
        self.height = height
        self.edgeInsets = edgeInsets
    }
    
}

/// 按钮边框：颜色、宽度
struct ButtonBorder {
    var color:Color
    var width:CGFloat
    
    init(color: Color, width: CGFloat) {
        self.color = color
        self.width = width
    }
    
}

/// 按钮样式集
struct ButtonStyleSet {
    /// 正常背景色
    fileprivate static let backgroundColor:[ButtonTypeEnum:Color] = [
        .primary:Color.g_color_theme,
        .secondary:Color.g_color_bg_white,
        .danger:Color.g_color_msg_red,
    ]
    
    /// 按压背景色
    fileprivate static let backgroundColorIsPressed:[ButtonTypeEnum:Color] = [
        .primary:Color.g_color_theme_dark,
        .secondary:Color.g_color_bg_dark,
        .danger:Color.g_color_msg_red_dark,
    ]
    
    /// 禁用背景色
    fileprivate static let backgroundColorIsDisabled:[ButtonTypeEnum:Color] = [
        .primary:Color.g_color_theme_grey,
        .secondary:Color.g_color_bg_white,
        .danger:Color.g_color_msg_red_light,
    ]
    
    /// 正常边框
    fileprivate static let border:[ButtonTypeEnum:ButtonBorder] = [
        .primary:ButtonBorder(color: Color.g_color_theme, width: CommonStyleSet.g_borderWidth),
        .secondary:ButtonBorder(color: Color.g_color_line, width: CommonStyleSet.g_borderWidth),
        .danger:ButtonBorder(color: Color.g_color_msg_red, width: CommonStyleSet.g_borderWidth),
    ]
    
    /// 按压边框
    fileprivate static let borderIsPressed:[ButtonTypeEnum:ButtonBorder] = [
        .primary:ButtonBorder(color: Color.g_color_theme_dark, width: CommonStyleSet.g_borderWidth),
        .secondary:ButtonBorder(color: Color.g_color_line, width: CommonStyleSet.g_borderWidth),
        .danger:ButtonBorder(color: Color.g_color_msg_red_dark, width: CommonStyleSet.g_borderWidth),
    ]
    
    /// 禁用边框
    fileprivate static let borderIsDisabled:[ButtonTypeEnum:ButtonBorder] = [
        .primary:ButtonBorder(color: Color.g_color_theme_grey, width: CommonStyleSet.g_borderWidth),
        .secondary:ButtonBorder(color: Color.g_color_line, width: CommonStyleSet.g_borderWidth),
        .danger:ButtonBorder(color: Color.g_color_msg_red_light, width: CommonStyleSet.g_borderWidth),
    ]
    
    /// 正常字体色
    fileprivate static let fontColor:[ButtonTypeEnum:Color] = [
        .primary:Color.g_color_font_white,
        .secondary:Color.g_color_font_primary,
        .danger:Color.g_color_font_white,
        .character:Color.g_color_theme,
    ]
    
    /// 按压字体色
    fileprivate static let fontColorIsPressed:[ButtonTypeEnum:Color] = [
        .primary:Color.g_color_font_white,
        .secondary:Color.g_color_font_primary,
        .danger:Color.g_color_font_white,
        .character:Color.g_color_theme_dark,
    ]
    
    /// 禁用字体色
    fileprivate static let fontColorIsDisabled:[ButtonTypeEnum:Color] = [
        .primary:Color.g_color_font_fadeout,
        .secondary:Color.g_color_font_disabled,
        .danger:Color.g_color_font_fadeout,
        .character:Color.g_color_theme_grey,
    ]
    
    /// 按钮字体样式
    fileprivate static let fontSize:[ButtonSizeEnum:Font] = [
        .large: .g_font_body_regular16,
        .medium: .g_font_description_regular14,
        .small: .g_font_description_regular14,
    ]
    
    /// 按钮宽度、高度、内边距
    static let buttonSize:[ButtonSizeEnum:ButtonSize] = [
        .large: ButtonSize(width: 158, height: 38, edgeInsets: EdgeInsets(top: 7, leading: 31, bottom: 7, trailing: 31)),
        .medium: ButtonSize(width: 114, height: 34, edgeInsets: EdgeInsets(top: 6, leading: 15, bottom: 6, trailing: 15)),
        .small: ButtonSize(width: 58, height: 26, edgeInsets: EdgeInsets(top: 3, leading: 8, bottom: 3, trailing: 8))
    ]
    
}

/// 按钮自定义样式
struct ButtonCustomStyle: ButtonStyle {
    @Environment(\.isEnabled) private var isEnabled: Bool
    var buttonSize:ButtonSizeEnum
    var buttonType:ButtonTypeEnum
    
    init(buttonSize: ButtonSizeEnum = .medium, buttonType: ButtonTypeEnum = .primary) {
        self.buttonSize = buttonSize
        self.buttonType = buttonType
    }
    
    public func makeBody(configuration: ButtonCustomStyle.Configuration) -> some View {
        // 设置按钮背景颜色、字体、字体颜色、圆角、边框颜色
        var backgroundColor = ButtonStyleSet.backgroundColor[buttonType]
        let fontSize = ButtonStyleSet.fontSize[buttonSize]
        var fontColor = ButtonStyleSet.fontColor[buttonType]
        let cornerRadius = CommonStyleSet.g_radius_small
        var border = ButtonStyleSet.border[buttonType]
        
        // 设置按压状态按钮背景颜色、字体颜色、边框颜色
        if configuration.isPressed {
            backgroundColor = ButtonStyleSet.backgroundColorIsPressed[buttonType]
            fontColor = ButtonStyleSet.fontColorIsPressed[buttonType]
            border = ButtonStyleSet.borderIsPressed[buttonType]
        }
        
        // 设置禁用状态按钮背景颜色、字体颜色、边框颜色
        if !isEnabled {
            backgroundColor = ButtonStyleSet.backgroundColorIsDisabled[buttonType]
            fontColor = ButtonStyleSet.fontColorIsDisabled[buttonType]
            border = ButtonStyleSet.borderIsDisabled[buttonType]
        }
        
        // 返回文字按钮
        if buttonType == .character {
            return AnyView(
                configuration.label
                    .font(fontSize)
                    .foregroundColor(fontColor)
            )
            
        }
        
        // 返回主要、次要、禁用按钮
        return AnyView(
            configuration.label
                .background(backgroundColor)
                .font(fontSize)
                .foregroundColor(fontColor)
                .cornerRadius(cornerRadius)
                .overlay(
                    RoundedRectangle(cornerRadius: cornerRadius, style: .continuous)
                        .stroke(border!.color, lineWidth: border!.width))
        )
        
    }
}
